<?php
/*
  Immanuel192 - ABSoft - 
  7/24/2009:   Fix to save section 
/*/
// Bail in case someone has visited us directly, or the Exponent framework is
// otherwise not initialized.
if (!defined('EXPONENT')) exit('');

$check_id = -1;
$section = null;
$old_parent = null;
if (isset($_POST['id'])) {
	// Saving an existing content page.  Read it from the database.
	$section = $db->selectObject('section','id='.intval($_POST['id']));
	if ($section) {
		$old_parent = $section->parent;
		$check_id = $section->id;
	}
}

// Update the section from the _POST data.
$section = section::update($_POST,$section); // Section ID updated
if ($check_id == -1) {
	$check_id = $section->parent;
}

if (($check_id != -1 && 
	exponent_permissions_check('manage',exponent_core_makeLocation('navigationmodule','',$check_id)) ||
	$user->is_acting_admin == 1
	)) {

	// make sure the SEF name is valid
        global $router;
        $temp=null;
        if (isset($section->id))
     		$temp= $db->selectObject('section','id='. $section->id );
	if ($_REQUEST['create_new'] == 1)
	{
		if ($temp)
		{
			validator::failAndReturnToForm('The ID specified in the PAGE ID field is a duplicate of an existing page.');
		}
	}
        
        if (empty($section->sef_name)) $section->sef_name = $router->encode($section->name);
        if (!section::isValidName($section->sef_name)) validator::failAndReturnToForm('You have invalid characters in the SEF Name field.');
        if (section::isDuplicateName($section)) validator::failAndReturnToForm('The name specified in the SEF Name field is a duplicate of an existing page.');
	if ($_REQUEST['create_new'] != 1) {

		if ($section->parent != $old_parent) {
			// Old_parent id was different than the new parent id.  Need to decrement the ranks
			// of the old children (after ours), and then add 
			$section = section::changeParent($section,$old_parent,$section->parent);
		}
	
		// Existing section.  Update the database record.
		// The 'id=x' where clause is implicit with an updateObject
		$db->updateObject($section,'section');
	} else {
		// Since this is new, we need to increment ranks, in case the user
		// added it in the middle of the level.
		$db->increment('section','rank',1,'rank > ' . $section->rank . ' AND parent=' . $section->parent);	
		$db->insertObject($section,'section');
	}
	
	exponent_sessions_clearAllUsersSessionCache('navigationmodule');
	//navigationmodule::checkForSectionalAdmins($section->id);
	// Go back to where we came from.  Probably the navigation manager.
	exponent_flow_redirect();
} else {
	echo exponent_http_error(403);
}

?>
